package com.travelshare.business.service.impl;

import java.util.List;
import java.util.Map;

import com.travelshare.common.utils.CommonUtils;
import com.travelshare.common.utils.DateUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.travelshare.business.mapper.ColumnDetailMapper;
import com.travelshare.business.domain.ColumnDetail;
import com.travelshare.business.domain.app.LbtData;
import com.travelshare.business.service.IColumnDetailService;
import com.travelshare.business.utils.CustomCacheUtils;

/**
 * 文章内容详情Service业务层处理
 * 
 * @author travelshare
 * @date 2024-02-22
 */
@Service
@SuppressWarnings("rawtypes")
public class ColumnDetailServiceImpl implements IColumnDetailService {
	
	@Autowired
	private ColumnDetailMapper columnDetailMapper;
	
	/**
	 * app端 查询文章内容详情
	 * 
	 * @param detailId 文章内容详情主键
	 * @return 文章内容详情
	 */
	@Override
	public ColumnDetail selectColumnDetailForAppByDetailId(Long detailId) {
		ColumnDetail cm = columnDetailMapper.selectColumnDetailByDetailId(detailId);
		ColumnDetail c = new ColumnDetail();
		c.setDetailId(detailId);
		c.setClickNum(cm.getClickNum() + 1);
		cm.setClickNum(c.getClickNum());
		if(cm.getContent().contains("/dev-api")) {
			cm.setContent(cm.getContent().replaceAll("/dev-api", ""));
		}
		columnDetailMapper.updateColumnDetail(c);
		return cm;
	}

	/**
	 * 查询文章内容详情
	 * 
	 * @param detailId 文章内容详情主键
	 * @return 文章内容详情
	 */
	@Override
	public ColumnDetail selectColumnDetailByDetailId(Long detailId) {
		ColumnDetail cm = columnDetailMapper.selectColumnDetailByDetailId(detailId);
		//ColumnDetail c = new ColumnDetail();
		//c.setDetailId(detailId);
		//c.setClickNum(cm.getClickNum() + 1);
		//columnDetailMapper.updateColumnDetail(c);
		//getClickNumFromRedis(cm);
		//CustomCacheUtils.incrementCacheMapValue(ConstantsCustom.detail_click_nums, cm.getCacheId(), 1);
		return cm;
	}

	/*
	 * private void getClickNumFromRedis(ColumnDetail cm) {
	 * if(CustomCacheUtils.getCacheDataMapValue(ConstantsCustom.detail_click_nums,
	 * cm.getCacheId()) != null) { cm.setClickNum((int)
	 * CustomCacheUtils.getCacheDataMapValue(ConstantsCustom.detail_click_nums,
	 * cm.getCacheId())); } }
	 */

	/**
	 * 查询文章内容详情列表
	 * 
	 * @param columnDetail 文章内容详情
	 * @return 文章内容详情
	 */
	@Override
	public List<ColumnDetail> selectColumnDetailList(ColumnDetail columnDetail) {
		return columnDetailMapper.selectColumnDetailList(columnDetail);
	}

	/**
	 * 新增文章内容详情
	 * 
	 * @param columnDetail 文章内容详情
	 * @return 结果
	 */
	@Override
	public int insertColumnDetail(ColumnDetail columnDetail) {
		columnDetail.setCreateTime(DateUtils.getNowDate());
		Map<Character, Map> map = CustomCacheUtils.getSensitiveCache();
		String content = CommonUtils.matchingString(columnDetail.getContent(), map);
		String title = CommonUtils.matchingString(columnDetail.getTitle(), map);
		columnDetail.setContent(content);
		columnDetail.setTitle(title);
		if(columnDetail.getIsShare() == null) {
			columnDetail.setIsShare(0);
		}
		if(columnDetail.getIsTop() == null) {
			columnDetail.setIsTop(0);
		}
		if(columnDetail.getIsLbt() == null) {
			columnDetail.setIsLbt(0);
		}
		if(columnDetail.getReleaseTime() == null) {
			columnDetail.setReleaseTime(DateUtils.getNowDate());
		}
		if(StringUtils.isNotBlank(columnDetail.getPicContent())) {
			String str = "<p style=\"text-align: center;\">";
			if(columnDetail.getPicContent().contains(",")) {
				String[] picPaths = columnDetail.getPicContent().split(",");
				for(String imageSrc : picPaths) {
					String imgTag = "<img src=\"" +  imageSrc + "\" />";
					if(imageSrc.startsWith("/dev-api")) {
						imgTag = "<img src=\"/dev-api" +  imageSrc + "\" />";
					}
					str += imgTag;
				}
			}else {
				String imgTag = "<img src=\"" + columnDetail.getPicContent() + "\" />";
				str += imgTag;
			}
			 str += "</p>";
			 columnDetail.setContent(str);
		}
		return columnDetailMapper.insertColumnDetail(columnDetail);
	}

	/**
	 * 修改文章内容详情
	 * 
	 * @param columnDetail 文章内容详情
	 * @return 结果
	 */
	@Override
	public int updateColumnDetail(ColumnDetail columnDetail) {
		Map<Character, Map> map = CustomCacheUtils.getSensitiveCache();
		if(StringUtils.isNotBlank(columnDetail.getContent())) {
			String content = CommonUtils.matchingString(columnDetail.getContent(), map);
			columnDetail.setContent(content);
		}
		if(StringUtils.isNotBlank(columnDetail.getTitle())) {
			String title = CommonUtils.matchingString(columnDetail.getTitle(), map);
			columnDetail.setTitle(title);
		}
		if(StringUtils.isNotBlank(columnDetail.getPicContent())) {
			String str = "<div style=\"text-align: center;\">";
			if(columnDetail.getPicContent().contains(",")) {
				String[] picPaths = columnDetail.getPicContent().split(",");
				for(String imageSrc : picPaths) {
					String imgTag = "<img src=\"" +  imageSrc + "\" />";
					if(imageSrc.startsWith("/dev-api")) {
						imgTag = "<img src=\"/dev-api" +  imageSrc + "\" />";
					}
					str += imgTag;
				}
			}else {
				String imgTag = "<img src=\"" + columnDetail.getPicContent() + "\" />";
				str += imgTag;
			}
			 str += "</div>";
			 columnDetail.setContent(str);
		}
		columnDetail.setUpdateTime(DateUtils.getNowDate());
		return columnDetailMapper.updateColumnDetail(columnDetail);
	}

	/**
	 * 批量删除文章内容详情
	 * 
	 * @param detailIds 需要删除的文章内容详情主键
	 * @return 结果
	 */
	@Override
	public int deleteColumnDetailByDetailIds(Long[] detailIds) {
		return columnDetailMapper.deleteColumnDetailByDetailIds(detailIds);
	}

	/**
	 * 删除文章内容详情信息
	 * 
	 * @param detailId 文章内容详情主键
	 * @return 结果
	 */
	@Override
	public int deleteColumnDetailByDetailId(Long detailId) {
		return columnDetailMapper.deleteColumnDetailByDetailId(detailId);
	}

	@Override
	public int checkColumnDetailByDetailIdsAndUserId(Long[] detailIds, Long userId) {
		return columnDetailMapper.checkColumnDetailByDetailIdsAndUserId(detailIds, userId);
	}

	@Override
	public int batchInfoColumnDetail(List<ColumnDetail> columnDetailList) {
		return columnDetailMapper.batchInfoColumnDetail(columnDetailList);
	}

	@Override
	public List<LbtData> selectLbtData(ColumnDetail columnDetail) {
		return columnDetailMapper.selectLbtData(columnDetail);
	}

	@Override
	public ColumnDetail selectColumnDetailByDetailIdAndUserId(Long detailId, Long userIds) {
		ColumnDetail cm = columnDetailMapper.selectColumnDetailByDetailIdAndUserId(detailId, userIds);
		ColumnDetail c = new ColumnDetail();
		c.setDetailId(detailId);
		if(cm.getContent().contains("/dev-api")) {
			cm.setContent(cm.getContent().replaceAll("/dev-api", ""));
		}
		if("Y".equals(cm.getState())) {
			c.setClickNum(cm.getClickNum() + 1);
			cm.setClickNum(c.getClickNum());
			columnDetailMapper.updateColumnDetail(c);
		}
		return cm;
	}
}
